<?php

  class ConsoleHelper {
  
    const OVERALL_INDENT = 10;
    
    static function CreateFile($filename, $content) {
      /* This needs to be revisited; global variable here doesn't make sense 
         Maybe using console as abstract method is quite serious error;
         Handling those methods through an object could give desired, extra flexibility
      */
      global $overwrite;
      $filename = APP_DIR . $filename;
      
      if (!file_exists($filename)) {
        $overwrite = 'y';
      } elseif ($overwrite == 'a') {
        $overwrite = 'a';
      } elseif ($overwrite == 'q') {
        $overwrite = 'q';
        self::SkipMessage($filename);
        return;
      } else {
        self::ExistsMessage("{$filename}\nDo you want to replace it with a new one? [Ynaq]");
        $line = self::ynaqAsk();
        $overwrite = $line;
      }
      if (in_array($overwrite, sa('y, a'))) {
        FileHandler::ForcedCreateFile($filename, $content);
        self::CreateMessage($filename);
      } elseif ($overwrite == 'n') {
        self::SkipMessage($filename);
      } else {
        self::SkipMessage($filename);
      }
    }
    
    static function HandleFileHanlderAnswer($path, $return_code) {
      if ($GLOBALS['overwrite'] == 'q') {
        $return_code = FileHandler::SKIP;
      }
      switch ($return_code) {
        case FileHandler::CREATED:
          self::CreateMessage($path);
          break;
        case FileHandler::EXISTS:
          self::ExistsMessage($path);
          break;
        case FileHandler::SKIP:
          self::SkipMessage($path);
          break;
        default:
          self::ErrorMessage($path);
          break;
      }
    }
    
    
    static function CreateMessage($path) {
      self::Message('create', $path);
    }
    
    static function SkipMessage($path) {
      self::Message('skip', $path);
    }
    
    static function ExistsMessage($path) {
      self::Message('exists', $path);
    }
    
    static function ErrorMessage($path) {
      self::Message('error', $path);
    }
    
    static function Message($prefix, $text) {
      $text_parts = explode("\n", $text);
      echo str_pad($prefix, self::OVERALL_INDENT, " ", STR_PAD_LEFT);
      echo "  {$text_parts[0]}\n";
      array_shift($text_parts);
      foreach ($text_parts as $text_part) {
        echo str_pad('', self::OVERALL_INDENT, " ", STR_PAD_LEFT);
        echo "  {$text_part}";
      }
    }
    
    static function CreateFolder ($path, $root_dir = true) {
      $dir = $root_dir ? APP_DIR . $path : $path;
      $return_code = FileHandler::CreateDirectory($dir);
      self::HandleFileHanlderAnswer($dir, $return_code);
    }
    
    static function ynaqAsk() {
      do {
        $line = strtolower(trim(fgets(STDIN)));
      } while (!in_array($line, sa('y,n,a,q')));
      echo "\n";
      return $line;
    }
    
  }

  
?>